<template>
   <div id="box">
      <div id="search" style="padding-left: 150px">
         <el-form :inline="true" :model="searchForm" class="demo-form-inline">
            <el-form-item label="班级">
               <el-input v-model="searchForm.clazz" placeholder="班级"></el-input>
            </el-form-item>
            <el-form-item>
               <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
         </el-form>
      </div>

      <div id="headerTool" style="padding-left: 150px;margin-bottom: 20px">
         <el-button type="primary" plain @click="addInfo">新增</el-button>
         <el-button type="danger" plain @click="deleteInfo">批量删除</el-button>
      </div>
      <!--   :data="tableData"    表格中需要渲染的数据
              label="日期"   表头
              width="180"   列宽
              prop="date"   当前列需要显示的数据
              @selection-change  点击复选框时间
       -->
      <el-table
          :data="tableData"
          @selection-change="handleSelectionChange"
          style="width: 80%;margin: 0 auto">
         <el-table-column
             type="selection"
             width="55">
         </el-table-column>

         <el-table-column
             prop="id"
             label="ID">
         </el-table-column>

         <el-table-column
             prop="clazzName"
             label="班级名">
         </el-table-column>
         <el-table-column
             prop="teacher"
             label="班主任">
         </el-table-column>

         <el-table-column
             prop="studentNumber"
             label="人数">
         </el-table-column>
         <el-table-column label="操作">
            <template slot-scope="scope">
               <el-button
                   size="mini"
                   @click="handleEdit(scope.row)">编辑</el-button>
               <el-button
                   size="mini"
                   type="danger"
                   @click="handleDelete(scope.row)">删除</el-button>
            </template>
         </el-table-column>

      </el-table>
      <div class="block" style="padding-left: 150px;margin-top: 20px">
         <!--      current-page: 当前页   page-size: 每页展示的条数    total 数据总条数  page-sizes 下拉选选项 -->
         <el-pagination
             @size-change="handleSizeChange"
             @current-change="handleCurrentChange"
             :current-page="1"
             :page-sizes="[5, 15, 20, 25]"
             :page-size="5"
             layout="total, sizes, prev, pager, next, jumper"
             :total="total">
         </el-pagination>
      </div>

      <el-dialog title="新增" :visible.sync="dialogFormVisible" @close="clearInfo" >
         <el-form :model="addForm" ref="addForm" :label-position="labelPosition" label-width="80px" :rules="rules">
            <el-form-item label="班级" prop="clazz">
               <el-input v-model="addForm.clazz" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="班主任" prop="teacher">
               <el-input v-model="addForm.teacher" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="人数" prop="studentNumber">
               <el-input v-model="addForm.studentNumber" autocomplete="off"></el-input>
            </el-form-item>

         </el-form>
         <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addStudent">确 定</el-button>
         </div>
      </el-dialog>


      <el-dialog title="编辑" :visible.sync="editDialogFormVisible" >
         <el-form :model="editForm" ref="editForm" :label-position="labelPosition" label-width="80px" :rules="rules">
            <el-form-item v-show="false" >
               <el-input v-model="editForm.id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="班级" prop="clazz">
               <el-input v-model="addForm.clazz" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="班主任" prop="teacher">
               <el-input v-model="addForm.teacher" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="人数" prop="studentNumber">
               <el-input v-model="addForm.studentNumber" autocomplete="off"></el-input>
            </el-form-item>
         </el-form>
         <div slot="footer" class="dialog-footer">
            <el-button @click="editDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="editStudent">确 定</el-button>
         </div>
      </el-dialog>

   </div>
</template>

<script>
export default {
    name: "ClazzView",
   data() {
      return {
         ids:[],
         labelPosition:'right',
         total:100,
         editDialogFormVisible:false,
         dialogFormVisible:false,
         editForm:{
            id:'',
            teacher:'',
            studentNumber:'',
            clazz:'',
         },
         addForm:{
            teacher:'',
            studentNumber:'',
            clazz:'',
         },
         searchForm:{
            clazz:'',
            page:1,
            limit:5
         },
         tableData: [],
         rules:{
           teacher: [
               { required: true, message: '请输入学生姓名', trigger: 'blur' },
            ],
            clazz: [
               { required: true, message: '请输入班级', trigger: 'blur' },
            ],
            studentNumber: [
               { required: true, message: '请输入联系方式', trigger: 'blur' },
            ]
         }
      }
   },
   methods:{
      // 清空表单检验规则
      clearInfo(){
         this.addForm = {
            teacher:'',
            studentNumber:'',
            clazz:'',
         }
         this.$refs.addForm.clearValidate()
         this.$refs.editForm.clearValidate()
      },
      editStudent(){

      },
      addStudent(){

      },
      handleEdit(row){
         this.editDialogFormVisible = true
         // 将当前行数据回显在表单中
         this.editForm = row
      },
      handleDelete(row){

         this.$confirm('确认删除当前行数据?', '删除', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
         }).then(() => {
            // 发送http请求  this.ids = []
            console.log(row)
         }).catch(() => {
            this.$message({
               type: 'info',
               message: '已取消删除'
            });
         });


      },
      handleSelectionChange(val){
         this.ids =  val.map(row=>row.id)

      },
      //  改变每页显示数据条数时 会自动调用该函数  参数:当前页允许显示数据的条数
      handleSizeChange(limit){
         this.searchForm.limit=limit
         this.clazzPage()
      },
      // 改变页码时会自动调用该函数    参数 :当前页的页码
      handleCurrentChange(currentPage){
         this.searchForm.page = currentPage
         this.clazzPage()
      },
      onSubmit(){
         // 在搜索之前初始化页码为第一页  以免查询看不到数据
         this.searchForm.page = 1
         this.clazzPage()
      },
      addInfo(){
         // 显示弹出层
         this.dialogFormVisible = true
      },
      deleteInfo(){

         // 判定用户是否已经选择的数据
         if (this.ids.length==0){
            this.$notify.warning("请至少选择一条数据")
            return false
         }


      },
      clazzPage(){

      }
   },
   created() {
      this.clazzPage()
   }
}
</script>

<style scoped>

</style>